<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Controller;
use OT\DataDictionary;

/**
 * 前台首页控制器
 * 主要获取首页聚合数据
 */
class ProductController extends HomeController {

	//系统首页页面跳转
    public function index(){
        
        $this->display();
    }
    //添加产品页面
    public function proadd(){
        $Types = D('Type')->select();
        $tlist = $Types;
        $this->assign('tlist',$tlist);

        $Product = D('Product')->select();
        // echo '<pre>';
        // print_r($Product);
        // exit;
        $num = count($Product);
        $this->assign('num',$num);
        $this->display();
    }

    public function proincrease(){
        $uid = $_SESSION['home']['user_auth']['uid'];
        $Company = D('Company')->where('uid='.$uid)->select();
        $cid = $Company[0]['id'];

        $data = $_POST;


        $Type = D('Type');
        $groupname = $Type->where('id='.$data['groupid'])->select();
        // echo '<pre>';
        // print_r($groupname);
        // exit;
        $data['groupname'] = $groupname[0]['title'];
        $keyword = $data['keyword'];
        $keyword = implode(',',$keyword);
        $data['keyword'] = $keyword;
        $data['cid'] = $cid;
        // echo '<pre>';
        // print_r($data);
        // exit;
        $Product = D('Product');
        $id = $Product->add($data);

        if($id){
            $uid = $_SESSION['home']['user_auth']['uid'];
            $Company = D('Company')->where('uid='.$uid)->select();
            $cid = $Company[0]['id'];
            $Product = D('Product');
            $list = $Product->where('cid='.$cid)->select();
            // echo '<pre>';
            // print_r($list);
            // exit;
            $this->assign('list',$list);
            
            $this->redirect('Product/proindex');
            $_SESSION['proid'] = $id; // 产品ID存入SEESSION
        } else {
            $this->error('新增失败');
        }
    }
        

    //所有产品
    public function proindex(){
        // echo '<pre>';
        // print_r($_SESSION);
        // exit;
       

        $uid = $_SESSION['home']['user_auth']['uid'];
        $Company = D('Company')->where('uid='.$uid)->select();
        $cid = $Company[0]['id'];
        // echo '<pre>';
        // print_r($cid);
        // print_R($_SESSION);
        // exit;
        $Product = D('Product');
        $list = $Product->where('cid='.$cid)->select();
        // echo '<pre>';
        // print_r($_SESSION);
        // print_r($list);
        // exit;

        // echo "<pre>";
        
        $this->assign('list',$list);
        $this->display();
    }
    //分类管理
    public function typeindex(){

        
        $uid = $_SESSION['home']['user_auth']['uid'];
        $Company = D('Company')->where('uid='.$uid)->select();
        $cid = $Company[0]['id'];
        // echo '<pre>';
        // print_r($_SESSION);
        // exit;
        $Type = D('Type');
        $list = $Type->where('cid='.$cid)->select();
        // echo '<pre>';
        // print_r($list);
        // exit;
        $this->assign('list',$list);
        $this->display();
    }
   

    public function typeadd(){
        $uid = $_SESSION['home']['user_auth']['uid'];
        $Company = D('Company')->where('uid='.$uid)->select();
        $cid = $Company[0]['id'];
        // echo '<pre>';
        // print_r($Company);
        // exit;

    	$Type = D('Type');
        $data = $Type->create($_POST);
        $data['cid'] = $cid;
      
        if($data){
            $id = $Type->add($data);
            if($id){
                // S('DB_CONFIG_DATA',null);
                //记录行为
               // action_log('update_Type', 'Type', $id, UID);
             //   $this->success('新增成功', Cookie('__forward__'));
			 $this->display("Product/typeindex");
            } else {
                $this->error('新增失败');
            }
        } else {
            $this->error($Type->getError());
        }
    }

    public function prodelmore(){
        $list = $_POST['prolist'];
        // print_r($list);

        $list = implode(',',$list);


        
        $Product = D('Product');

        $sql = $Product->query('select path from onethink_product where id in('.$list.')');

        $path = $sql;
        foreach($path as $k=>$v){
            // echo $v['path'];
            unlink($_SERVER['DOCUMENT_ROOT'].$v['path']); //删除多张图
        }
        // echo'<pre>';
        // print_r($path);

        
        $result = $Product->execute('delete from onethink_product where id in('.$list.')');

    }

    // -----------------------------------------------------------

    public function typedelmore(){
        // $data = $_POST;
        // echo '<pre>';
        // print_r($data);
        // exit;

        $list = $_POST['typelist'];
        $list = implode(',',$list);

        $Product = D('Product');
        $res = $Product->query('select id from onethink_product where groupid in('.$list.')');
        $res = count($res);
        // echo '<pre>';
        // echo $res;
        // exit;
        if($res>0){
            die();
        }else{
            $Type = D('Type');
            $r = $Type->query('select title from onethink_type where id in('.$list.')');
            $result = $Type->execute('delete from onethink_type where id in('.$list.')');
            print_r($r); 
        }

// --------------------------------------------------------------       

    }

    public function import(){
        $this->display('import.html');
    }


    public function session(){
         $_SESSION = $_SESSION;
         $id = $_POST;
         $_SESSION['proid'] = $id;



        // exit;
    }








    /* 文件上传 */
    public function upload(){

        $return  = array('status' => 1, 'info' => '上传成功', 'data' => '');
        /* 调用文件上传组件上传文件 */
        $File = D('Home/File');
        $file_driver = C('DOWNLOAD_UPLOAD_DRIVER');
        $info = $Product->upload(
            $_FILES,
            C('DOWNLOAD_UPLOAD'),
            C('DOWNLOAD_UPLOAD_DRIVER'),
            C("UPLOAD_{$file_driver}_CONFIG")
        );

        /* 记录附件信息 */
        if($info){
            $return['data'] = think_encrypt(json_encode($info['download']));
            $return['info'] = $info['download']['name'];
        } else {
            $return['status'] = 0;
            $return['info']   = $Product->getError();
        }

        /* 返回JSON数据 */
        $this->ajaxReturn($return);
    }

    /* 下载文件 */
    public function download($id = null){
        if(empty($id) || !is_numeric($id)){
            $this->error('参数错误！');
        }

        $logic = D('Download', 'Logic');
        if(!$logic->download($id)){
            $this->error($logic->getError());
        }

    }

    /**
     * 上传图片
     * @author huajie <banhuajie@163.com>
     */
    public function uploadPicture(){
        //TODO: 用户登录检测
        echo "string";
        /* 返回标准数据 */
        $return  = array('status' => 1, 'info' => '上传成功', 'data' => '');

        /* 调用文件上传组件上传文件 */
        $Product = D('Home/Product');
        // echo '<pre>';
        // print_r($Product);
        // exit;
        $pic_driver = C('PICTURE_UPLOAD_DRIVER');
        $info = $Product->upload(
            $_FILES,
            C('PICTURE_UPLOAD'),
            C('PICTURE_UPLOAD_DRIVER'),
            C("UPLOAD_{$pic_driver}_CONFIG")
        ); //TODO:上传到远程服务器

        /* 记录图片信息 */
        if($info){
            $return['status'] = 1;
            $return = array_merge($info['download'], $return);
        } else {
            $return['status'] = 0;
            $return['info']   = $Product->getError();
        }

        /* 返回JSON数据 */
        $this->ajaxReturn($return);

    }


}